package com.hejing.bpeldesign.bpelprase;


import com.hejing.bpeldesign.model.ReceiveNode;
import com.hejing.bpeldesign.model.BpelNode;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import java.util.UUID;

public class ReceiveNodeParser implements NodeParserStrategy {

    @Override
    public BpelNode parse(Element el) {
        String id = UUID.randomUUID().toString();
        String name = el.getAttribute("name");
        String bounds = el.getAttribute("bounds");
        String[] parts = bounds.split(",");
        int x = Integer.parseInt(parts[0]);
        int y = Integer.parseInt(parts[1]);
        int width = Integer.parseInt(parts[2]);
        int height = Integer.parseInt(parts[3]);

        ReceiveNode node = new ReceiveNode(id, name, el.getTagName(), x, y, width, height);

        NodeList inputs = el.getElementsByTagName("input");
        for (int i = 0; i < inputs.getLength(); i++) {
            Element input = (Element) inputs.item(i);
            String variable = input.getAttribute("variable");
            String type = input.getAttribute("type");
            node.addInput(variable, type);
        }

        return node;
    }
}
